<template>
  <div :style="{ padding: '0 0 32px 32px' }">
    <h4 :style="{ marginBottom: '20px' }">{{ title }}</h4>
    <v-chart
      height="254"
      :data="data"
      :scale="scale"
      :forceFit="true"
      :padding="['auto', 'auto', 'auto', '50']">
      <v-tooltip />
      <v-axis />
      <v-bar position="x*y"/>
    </v-chart>
  </div>
</template>

<script>

  const sourceData = []
  for (let i = 0; i < 12; i += 1) {
    sourceData.push({
      x: `${i + 1}月`,
      y: Math.floor(Math.random() * 1000) + 200
    })
  }
  const tooltip = [
    'x*y',
    (x, y) => ({
      name: x,
      value: y
    })
  ]
  // const scale = [{
  //   dataKey: 'x',
  //   min: 2
  // }, {
  //   dataKey: 'y',
  //   title: '时间',
  //   min: 1,
  //   max: 22
  // }]

  export default {
    name: "Bar",
    props: {
      title: {
        type: String,
        default: ''
      },
      dataSource: {
        type: Array,
        default: () => []
      },
      // x 轴别名
      x: {
        type: String,
        default: 'x'
      },
      // y 轴别名
      y: {
        type: String,
        default: 'y'
      },
    },
    computed:{
      scale() {
        return [
          { dataKey: 'x', title: this.x, alias: this.x },
          { dataKey: 'y', title: this.y, alias: this.y }
        ]
      }
    },
    mounted(){
    },
    watch:{
      dataSource:{
        deep: true,
        handler(val){
          if(val && val.length>0){
            this.data = val
          }else{
            this.data = sourceData;
          }
        }
      }
    },
    created() {
      if (this.dataSource.length === 0) {
        this.data = sourceData
      } else {
        this.data = this.dataSource
      }
    },
    data () {
      return {
        data:[],
        tooltip
      }
    }
  }
</script>